package com.szd.qiongyou20.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.bean.HomeListData;
import com.szd.qiongyou20.utils.BitmapCircleTransformation;

import java.util.List;


/**
 * Created by SZD on 2016/12/6.
 */

public class RecyclerView_Home_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<HomeListData.DataBean.FeedBean.EntryBean> mList;
    private LayoutInflater mInflater;

    private static final int Author = 1, Default = 2;

    public RecyclerView_Home_Adapter(Context context, List<HomeListData.DataBean.FeedBean.EntryBean> list) {
        mContext = context;
        mList = list;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getAuthor().getUsername() != null)
            return Author;
        return Default;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case Author:
                view = mInflater.inflate(R.layout.item_home_author, parent, false);
                return new ViewHolder_Author(view);
            case Default: {
                view = mInflater.inflate(R.layout.item_home_default, parent, false);
                return new ViewHolder_Default(view);
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder_Author) {
            // 获取分类
            String column = mList.get(position).getColumn();
            // 获取图片的地址
            String cover = mList.get(position).getCover();
            // 获取作者头像的地址
            String pic = mList.get(position).getAuthor().getPic();
            // 获取作者的昵称
            String username = mList.get(position).getAuthor().getUsername();
            // 获取标题
            String title = mList.get(position).getTitle();
            // 获取内容
            String subject = mList.get(position).getSubject();
            // 获取下部icon的地址
            String icon_url = mList.get(position).getIcon_url();
            // 给控件赋值
            ((ViewHolder_Author) holder).mTextView_item_a_column.setText(column);
            ((ViewHolder_Author) holder).mTextView_item_a_title.setText(title);
            ((ViewHolder_Author) holder).mTextView_item_a_subject.setText(subject);
            ((ViewHolder_Author) holder).mTextView_item_a_name.setText(username);
            Glide.with(mContext).load(cover).into(((ViewHolder_Author) holder).mImageView_item_a_show);
            Glide.with(mContext)
                    .load(pic).transform(new BitmapCircleTransformation(mContext))
                    .into(((ViewHolder_Author) holder).mImageView_item_a_head);
            Glide.with(mContext).load(icon_url).into(((ViewHolder_Author) holder).mImageView_item_a_logo);
        }
        if (holder instanceof ViewHolder_Default) {
            // 获取分类
            String column = mList.get(position).getColumn();
            // 获取图片的地址
            String cover = mList.get(position).getCover();
            // 获取标题
            String title = mList.get(position).getTitle();
            // 获取内容
            String subject = mList.get(position).getSubject();
            // 获取底部的icon
            String icon_url = mList.get(position).getIcon_url();
            // 给控件赋值
            ((ViewHolder_Default) holder).mTextView_item_column.setText(column);
            ((ViewHolder_Default) holder).mTextView_item_title.setText(title);
            ((ViewHolder_Default) holder).mTextView_item_subject.setText(subject);
            Glide.with(mContext).load(cover).into(((ViewHolder_Default) holder).mImageView_item_show);
            Glide.with(mContext).load(icon_url).into(((ViewHolder_Default) holder).mImageView_item_logo);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder_Default extends RecyclerView.ViewHolder {

        private TextView mTextView_item_column, mTextView_item_title, mTextView_item_subject;
        private ImageView mImageView_item_show, mImageView_item_logo;

        public ViewHolder_Default(View itemView) {
            super(itemView);
            mTextView_item_column = (TextView) itemView.findViewById(R.id.textView_item_type);
            mTextView_item_title = (TextView) itemView.findViewById(R.id.textView_item_title);
            mTextView_item_subject = (TextView) itemView.findViewById(R.id.textView_item_content);
            mImageView_item_show = (ImageView) itemView.findViewById(R.id.imageView_item_show);
            mImageView_item_logo = (ImageView) itemView.findViewById(R.id.imageView_item_logo);
        }
    }

    class ViewHolder_Author extends RecyclerView.ViewHolder {

        private TextView mTextView_item_a_column, mTextView_item_a_title,
                mTextView_item_a_subject, mTextView_item_a_name;
        private ImageView mImageView_item_a_show, mImageView_item_a_head, mImageView_item_a_logo;

        public ViewHolder_Author(View itemView) {
            super(itemView);
            mTextView_item_a_column = (TextView) itemView.findViewById(R.id.textView_item_a_type);
            mTextView_item_a_title = (TextView) itemView.findViewById(R.id.textView_item_a_title);
            mTextView_item_a_subject = (TextView) itemView.findViewById(R.id.textView_item_a_content);
            mTextView_item_a_name = (TextView) itemView.findViewById(R.id.textView_item_a_uerName);
            mImageView_item_a_show = (ImageView) itemView.findViewById(R.id.imageView_item_a_show);
            mImageView_item_a_head = (ImageView) itemView.findViewById(R.id.imageView_item_a_head);
            mImageView_item_a_logo = (ImageView) itemView.findViewById(R.id.imageView_item_a_logo);
        }
    }
}
